package com.demo.util;

import org.springframework.http.HttpHeaders;
import org.springframework.web.util.UriUtils;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

public class DownloadUtil {

    public static void setDownLoadFilename(HttpServletResponse response, String fileName) {
        String isoFileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        String utf8FileName = UriUtils.encode(fileName, "UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition, Content-Type");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment, filename=\"" + isoFileName + "\", filename=\"UTF-8''" + utf8FileName + "\"");
    }

}
